package App.Controller;

import App.Model.BaseEnum;
import App.Model.BaseResponse;
import App.Model.DataResponse;
import App.Model.Friend.*;
import App.Service.AccountService;
import App.Service.FriendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

/**
 * Created by xw on 2019/1/14.
 */


@RestController
@RequestMapping("/api/friend/")
public class FriendController extends BaseController {
    @Resource
    private AccountService _accountService;

    @Autowired
    private FriendService _service;

    //联系人列表，需要带搜索功能
    @RequestMapping("list")
    public ResponseEntity<?> listFriends(@RequestBody(required = false) FriendListRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<FriendListResponse> _result = _service.getFriendList(_request, userId());
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            return null;
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    //添加联系人好友
    @RequestMapping("save")
    public ResponseEntity<BaseResponse> saveFriend(@RequestBody(required = false) FriendSaveRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");

        System.out.println("step 1");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }

        System.out.println("step 2");

        try {
            System.out.println("step 3");

            int result = _service.insertFriend(_request, userId());
            _baseResponse.setMessage("保存失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            if (result > 0) {
                _baseResponse.setMessage("保存成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            System.out.println("step 4");

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


    //删除联系人好友
    @RequestMapping("delete")
    public ResponseEntity<BaseResponse> deleteFriend(@RequestBody(required = false) FriendDeleteRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int result = _service.deleteFriend(_request, userId());
            _baseResponse.setMessage("删除失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            if (result > 0) {
                _baseResponse.setMessage("删除成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


    //联系人列表，需要带搜索功能
    @RequestMapping("contacts")
    public ResponseEntity<?> listContacts(@RequestBody(required = false) FriendListRequest _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<FriendContactListResponse> _result = _service.getContactList(_request, userId());
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            return null;
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    //个人信息
    @RequestMapping("me")
   // @RequestMapping(value="me", method= RequestMethod.POST)
    //public void addEmployee(@RequestBody Optional<Employee> employee)
    public ResponseEntity<?> getMeInfo(
            @RequestHeader(value="version",required = false) String version,
            @RequestHeader(value="device",required = false) String device,
            @RequestBody(required = false) Optional<GetMeInfoRequest> _request) {
    //public ResponseEntity<?> getMeInfo(@RequestBody Optional<GetMeInfoRequest> _request) {
        DataResponse _dataResponse = new DataResponse();
        try {
            int _theUserId=userId();

            if(_request.isPresent()){

                String _requestUserId=  _request.get().getUserid();
                if(_requestUserId!=null&&_requestUserId!=""){
                    _theUserId=Integer.parseInt(_requestUserId);
                }
            }

            FriendMeResponse _result = _service.getUserInfo(_theUserId, userId());

            if(version != null && device != null){
                int userId = _theUserId;
                int clientTypeInt = BaseEnum.ClientType.getClientTypeStr(device);
                String optMethod = "me";
                if(_result != null ){
                    _accountService.insertUserLoginHistory(userId, version,
                            clientTypeInt, BaseEnum.ResultType.Success.getResultType(), optMethod);
                }else{
                    _accountService.insertUserLoginHistory(userId, version,
                            clientTypeInt, BaseEnum.ResultType.Fail.getResultType(), optMethod);
                }
            }

            _dataResponse.setData(_result);
        } catch (Exception ex) {
            return null;
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }


    //保存个人信息
    @RequestMapping("saveme")
    public ResponseEntity<BaseResponse> saveMe(@RequestBody(required = false) FriendMeSaveRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int result = _service.updateUserMeInfo(_request, userId());
            _baseResponse.setMessage("保存失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            if (result > 0) {
                _baseResponse.setMessage("保存成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            System.out.println("step 4");

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //我收到的好友申请
    @RequestMapping("applylist")
    public ResponseEntity<?> listContactApply() {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<FriendListResponse> _result = _service.getContactApplyList(userId());
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            return null;
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    //新增好友申请
    @RequestMapping("saveapply")
    public ResponseEntity<BaseResponse> saveContractApply(@RequestBody(required = false) FriendContractApplySaveRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int result = _service.inertContactApply(_request, userId());
            _baseResponse.setMessage("保存失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            if (result > 0) {
                _baseResponse.setMessage("保存成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            System.out.println("step 4");

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    //通过好友申请
    @RequestMapping("passapply")
    public ResponseEntity<BaseResponse> passContractApply(@RequestBody(required = false) FriendApplyPassRequest _request) {

        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {

            int _applyStatus = BaseEnum.ContactApplyStatus.Pass.getContactApplyStatus();
            int result = _service.updateContactApplyStatus(_request, _applyStatus);
            _baseResponse.setMessage("保存失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
            if (result > 0) {
                _baseResponse.setMessage("保存成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            System.out.println("step 4");

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }


    @RequestMapping("applycount")
    public ResponseEntity<BaseResponse> getUnReadFriendApply() {

        DataResponse _dataResponse = new DataResponse();
        try {
            int _result = _service.getUnReadFriendApply(userId());
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            return null;
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }

    @RequestMapping("sethasviewed")
    public ResponseEntity<BaseResponse> setHasViewed() {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        try {
            _service.setApplyHasViewed(userId());
            _baseResponse.setMessage("保存失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    @RequestMapping("addfile")
    public ResponseEntity<?> addExpertiseFile(@RequestBody(required = false) CrmUserExpertiseItemRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.addExpertiseFile(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    @RequestMapping("deletefile")
    public ResponseEntity<?> deleteExpertiseFile(@RequestBody(required = false) CrmUserExpertiseItemDeleteRequest _request) {
        BaseResponse _baseResponse = ResponseError(null, "参数格式错误");
        if (_request == null) {
            return new ResponseEntity(_baseResponse, HttpStatus.OK);
        }
        try {
            int _result = _service.deleteExpertiseFile(_request, userId());
            _baseResponse.setMessage("操作失败");
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());

            if (_result > 0) {
                _baseResponse.setMessage("操作成功");
                _baseResponse.setSuccess(BaseEnum.ApiResult.Success.getApiResult());
            }
            return new ResponseEntity(_baseResponse, HttpStatus.OK);

        } catch (Exception ex) {
            ex.printStackTrace();
            _baseResponse.setMessage("操作失败");
            _baseResponse.setData(ex.getMessage());
            _baseResponse.setSuccess(BaseEnum.ApiResult.Failure.getApiResult());
        }
        return new ResponseEntity(_baseResponse, HttpStatus.OK);
    }

    @RequestMapping("getfile")
    public ResponseEntity<?> getExpertiseFile() {
        DataResponse _dataResponse = new DataResponse();
        try {
            List<CrmUserExpertiseResponse> _result = _service.getExpertisesList(userId());
            _dataResponse.setData(_result);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return new ResponseEntity(_dataResponse, HttpStatus.OK);
    }
}